//
//  GS_EffectivenessDetailsViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//
#define URL(name, server) [NSString stringWithFormat:@"http://115.29.143.211/lol/apiMobile.php?action=getPlayersInfo&target=%@&serverName=%@", name, server]
#define HEROHEADIMAGE(heroName) [NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg", heroName]

#import "GS_EffectivenessDetailsViewController.h"
#import "GoodManNews_AFNetTool.h"
#import "GS_EffTableViewCell.h"
#import "GS_InfornationTableViewCell.h"

@interface GS_EffectivenessDetailsViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) UIView *headView;
@property (nonatomic, retain) UILabel *nameL;
@property (nonatomic, retain) UILabel *serverL;
@property (nonatomic, retain) UILabel *gradeL;
@property (nonatomic, retain) UILabel *goodL;
@property (nonatomic, retain) UILabel *zanL;
@property (nonatomic, retain) UILabel *effectivenessL;
@property (nonatomic, retain) UILabel *effL;
@property (nonatomic, retain) UILabel *allL;
@property (nonatomic, retain) NSMutableDictionary *dic;
@property (nonatomic, retain) UILabel *transL;
@property (nonatomic, retain) UILabel *winPointL;
@property (nonatomic, retain) NSString *firStr;
@property (nonatomic, retain) NSString *secStr;
@property (nonatomic, retain) NSString *thiStr;
@property (nonatomic, retain) NSString *fouStr;
@property (nonatomic, retain) NSString *fifStr;
@property (nonatomic, retain) NSMutableDictionary *botDic;
@property (nonatomic, retain) NSMutableDictionary *normalDic;
@property (nonatomic, retain) NSMutableDictionary *aramDic;
@property (nonatomic, retain) NSMutableDictionary *rankedDic;
@property (nonatomic, retain) UIImageView *imageV;




@end

@implementation GS_EffectivenessDetailsViewController

- (void)dealloc
{
    [_server release];
    [_name release];
    [_tableV release];
    [_headView release];
    [_nameL release];
    [_serverL release];
    [_gradeL release];
    [_goodL release];
    [_effectivenessL release];
    [_zanL release];
    [_allL release];
    [_dic release];
    [_effL release];
    [_allL release];
    [_transL release];
    [_winPointL release];
    [_fifStr release];
    [_firStr release];
    [_thiStr release];
    [_fouStr release];
    [_botDic release];
    [_imageV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
//    NSLog(@"name = %@", self.name);
//    NSLog(@"server = %@", self.server);
    [self createTableView];
    [self getData];
}

- (void)createTableView

{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.tableV];
    [_tableV release];
    
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
        
    }];

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.headView = [UIImageView new];
//    self.headView.backgroundColor = [UIColor greenColor];
    self.tableV.tableHeaderView = self.headView;
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.tableV);
        make.width.mas_equalTo(self.tableV);
        make.height.mas_equalTo(@200);
        
    }];
    
    self.nameL = [UILabel new];
//    self.nameL.backgroundColor = [UIColor purpleColor];
    self.nameL.font = [UIFont systemFontOfSize:16];
    self.nameL.textColor = [UIColor orangeColor];
    [self.headView addSubview:self.nameL];
    
    [self.nameL mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.headView).with.offset(15);
        make.top.equalTo(self.headView).with.offset(15);
        make.width.equalTo(@150);
        make.height.equalTo(@30);
        
    }];
    
    self.serverL = [UILabel new];
//    self.serverL.backgroundColor = [UIColor purpleColor];
    self.serverL.font = [UIFont systemFontOfSize:16];
    self.serverL.textColor = [UIColor whiteColor];
    [self.headView addSubview:self.serverL];
    
    [self.serverL mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.nameL.mas_right).with.offset(15);
        make.top.equalTo(self.headView).with.offset(20);
        make.width.equalTo(@65);
        make.height.equalTo(@25);
        
        
    }];
    
    self.gradeL = [UILabel new];
//    self.gradeL.backgroundColor = [UIColor orangeColor];
    self.gradeL.font = [UIFont systemFontOfSize:15];
    self.gradeL.textColor = [UIColor whiteColor];
    [self.headView addSubview:self.gradeL];
    
    [self.gradeL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.headView).with.offset(15);
        make.top.equalTo(self.nameL.mas_bottom).with.offset(10);
        make.width.equalTo(@80);
        make.height.equalTo(@25);
    }];
    
    self.winPointL = [UILabel new];
//    self.winPointL.backgroundColor = [UIColor orangeColor];
    self.winPointL.font = [UIFont systemFontOfSize:15];
    self.winPointL.textColor = [UIColor whiteColor];
    [self.headView addSubview:self.winPointL];
    
    [self.winPointL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.gradeL.mas_right).with.offset(15);
        make.top.equalTo(self.nameL.mas_bottom).with.offset(10);
        make.width.equalTo(@60);
        make.height.equalTo(@25);
    }];
    
    self.zanL = [UILabel new];
//    self.zanL.backgroundColor = [UIColor purpleColor];
    self.zanL.font = [UIFont systemFontOfSize:14];
    self.zanL.textColor = [UIColor whiteColor];
    [self.headView addSubview:self.zanL];
    
    [self.zanL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.headView).with.offset(15);
        make.top.equalTo(self.gradeL.mas_bottom).with.offset(10);
        make.width.equalTo(@62);
        make.height.equalTo(@25);
        
    }];
    
    self.goodL = [UILabel new];
//    self.goodL.backgroundColor = [UIColor redColor];
    self.goodL.font = [UIFont systemFontOfSize:14];
    self.goodL.textColor = [UIColor whiteColor];
    [self.headView addSubview:self.goodL];
    
    [self.goodL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.zanL.mas_right);
        make.top.equalTo(self.gradeL.mas_bottom).with.offset(10);
        make.width.equalTo(@40);
        make.height.equalTo(@25);
    
    }];
    
    self.effL = [UILabel new];
//    self.effL.backgroundColor = [UIColor redColor];
    self.effL.font = [UIFont systemFontOfSize:14];
    self.effL.textColor = [UIColor whiteColor];
    [self.headView addSubview:self.effL];
    
    [self.effL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.goodL.mas_right).with.offset(10);
        make.top.equalTo(self.gradeL.mas_bottom).with.offset(10);
        make.width.equalTo(@50);
        make.height.equalTo(@25);
        
    }];
    
    self.effectivenessL= [UILabel new];
//    self.effectivenessL.backgroundColor = [UIColor redColor];
    self.effectivenessL.font = [UIFont systemFontOfSize:14];
    self.effectivenessL.textColor = [UIColor whiteColor];
    [self.headView addSubview:self.effectivenessL];
    
    [self.effectivenessL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.effL.mas_right);
        make.top.equalTo(self.gradeL.mas_bottom).with.offset(10);
        make.width.equalTo(@50);
        make.height.equalTo(@25);
        
    }];
    
    self.allL= [UILabel new];
//    self.allL.backgroundColor = [UIColor grayColor];
    self.allL.font = [UIFont systemFontOfSize:15];
    self.allL.textAlignment = NSTextAlignmentLeft;
    self.allL.textColor = [UIColor whiteColor];
    [self.headView addSubview:self.allL];
    
    [self.allL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.headView).with.offset(15);
        make.top.equalTo(self.effectivenessL.mas_bottom).with.offset(10);
        make.width.equalTo(@355);
        make.height.equalTo(@25);
        
    }];
    
    
    self.transL= [UILabel new];
//    self.transL.backgroundColor = [UIColor grayColor];
    self.transL.font = [UIFont systemFontOfSize:14 ];
    self.transL.textAlignment = NSTextAlignmentLeft;
    self.transL.textColor = [UIColor whiteColor];
    [self.headView addSubview:self.transL];
    [self.transL mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.headView).with.offset(15);
        make.top.equalTo(self.allL.mas_bottom).with.offset(10);
        make.width.equalTo(@355);
        make.height.equalTo(@25);
        
    }];
    self.nameL.text = self.name;
    self.serverL.text = self.server;
    CGRect frame = [self.nameL.text boundingRectWithSize:CGSizeMake(1000, 35) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName]  context:nil];
    [self.nameL mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(@(frame.size.width));
        
    }];
    
    CGRect serverFrame = [self.serverL.text boundingRectWithSize:CGSizeMake(1000, 35) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName]  context:nil];
    [self.serverL mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(@(serverFrame.size.width));
        
    }];
    
    NSMutableDictionary *globDic = [self.dic objectForKey:@"global"];
    NSMutableDictionary *zdlDic = [self.dic objectForKey:@"zdl"];
    NSMutableDictionary *warzone = [self.dic objectForKey:@"warzone"];
    //    NSMutableDictionary *mostHeroDic = [self.dic objectForKey:@"mostUsedHeros"];
    
    NSNumber *rank = [[warzone objectForKey:@"R_S_5"] objectForKey:@"rank"];
    NSNumber *tier = [[warzone objectForKey:@"R_S_5"] objectForKey:@"tier"];
    NSNumber *winpoint = [[warzone objectForKey:@"R_S_5"] objectForKey:@"league_points"];
    
    NSString *grade = nil;
    NSString *no = nil;
    if ([rank isEqual:@0]) {
        no = @"I";
    } else if ([rank isEqual:@1]) {
        no = @"II";
    } else if ([rank isEqual:@2]) {
        no = @"IIV";
    } else if ([rank isEqual:@3]) {
        no = @"IV";
    } else if ([rank isEqual:@4]) {
        no = @"V";
    }
    
    if ([tier isEqual:@1]) {
        grade = @"英勇黄铜";
    } else if ([tier isEqual:@2]) {
        grade = @"不屈白银";
    } else if ([tier isEqual:@3]) {
        grade = @"荣耀黄金";
    } else if ([tier isEqual:@4]) {
        grade = @"华贵铂金";
    } else if ([tier isEqual:@5]) {
        grade = @"璀璨钻石";
    } else if ([tier isEqual:@6]) {
        grade = @"超凡大师";
    } else if ([tier isEqual:@7]) {
        grade = @"最强王者";
    }
    //    NSLog(@"=======%@", self.firStr);
    
    self.zanL.text = @"被赞次数:";
    self.effL.text = @"战斗力:";
    self.goodL.text = [globDic objectForKey:@"good"];
    self.effectivenessL.text = [zdlDic objectForKey:@"totalScore"];
    self.allL.text = [NSString stringWithFormat:@"%@ = %@ + %@ + %@", [zdlDic objectForKey:@"totalScore"], [zdlDic objectForKey:@"eloScore"], [zdlDic objectForKey:@"winRatioScore"], [zdlDic objectForKey:@"winBattleScore"]];
    
    self.gradeL.text = [NSString stringWithFormat:@"%@%@", grade, no];
    self.winPointL.text = [NSString stringWithFormat:@"胜点:%@", winpoint];
    self.transL.text = @"战斗力 = 基础分 + 胜率分 + 胜场分";
    
    
    return self.headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 200;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (0 == indexPath.row) {
        return 120;
    } else {
        return 80;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return 5;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (0 == indexPath.row) {
        NSString *effStr = @"effCell";
        GS_EffTableViewCell *effCell = [tableView dequeueReusableCellWithIdentifier:effStr];
        if (nil == effCell) {
            effCell = [[GS_EffTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:effStr];
            
        }
        effCell.titleL.text = @"最近常用英雄";
        effCell.firstImageV.image = [UIImage imageNamed:self.firStr];
        effCell.secondImageV.image = [UIImage imageNamed:self.secStr];
        effCell.thirdImageV.image = [UIImage imageNamed:self.thiStr];
        effCell.fourthImageV.image = [UIImage imageNamed:self.fouStr];
        effCell.fifthImageV.image = [UIImage imageNamed:self.fifStr];
        
        return effCell;
    } else {
        NSString *str = @"cell";
        GS_InfornationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (nil == cell) {
            cell = [[GS_InfornationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        if (1 == indexPath.row) {
            cell.imageV.image = [UIImage imageNamed:@"icon_duanwei_04"];
            cell.titleL.text = @"人机对战";
            cell.winL.text = [NSString stringWithFormat:@"胜场:%@", [self.botDic objectForKey:@"b_w"]];
            cell.loseL.text = [NSString stringWithFormat:@"负场:%@", [self.botDic objectForKey:@"b_lo"]];
        } else if (2 == indexPath.row) {
            cell.imageV.image = [UIImage imageNamed:@"icon_duanwei_05"];
            cell.winL.text = [NSString stringWithFormat:@"胜场:%@", [self.normalDic objectForKey:@"w"]];
            cell.loseL.text = [NSString stringWithFormat:@"负场:%@", [self.normalDic objectForKey:@"lo"]];
            cell.titleL.text = @"经典对战";
        } else if (3 == indexPath.row) {
            cell.imageV.image = [UIImage imageNamed:@"icon_duanwei_06"];
            cell.winL.text = [NSString stringWithFormat:@"胜场:%@", [self.aramDic objectForKey:@"a_w"]];
            cell.loseL.text = [NSString stringWithFormat:@"负场:%@", [self.aramDic objectForKey:@"a_lo"]];
            cell.titleL.text = @"极地大乱斗";
        } else if (4 == indexPath.row) {
            cell.imageV.image = [UIImage imageNamed:@"icon_duanwei_07"];
            cell.winL.text = [NSString stringWithFormat:@"胜场:%@", [self.rankedDic objectForKey:@"R_S_5_w"]];
            cell.loseL.text = [NSString stringWithFormat:@"负场:%@", [self.rankedDic objectForKey:@"R_S_5_lo"]];
            cell.titleL.text = @"排位赛";
        }
//        cell.backgroundColor = [UIColor yellowColor];
        return cell;
    }
    
}

- (void)getData
{
    self.dic = [NSMutableDictionary dictionary];
    [GoodManNews_AFNetTool getNetWithURL:URL(self.name, self.servserT) body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = [responseObject objectForKey:self.name];
        
        [self.dic addEntriesFromDictionary:dic];
        
        
        
//        NSLog(@"%@", self.dic);
        
        NSMutableDictionary *mostHeroDic = [self.dic objectForKey:@"mostUsedHeros"];
        NSMutableArray *heroArr = [NSMutableArray arrayWithArray:mostHeroDic.allKeys];
//        NSLog(@"%@", mostHeroDic.allKeys);
        self.firStr = heroArr[0];
        self.secStr = heroArr[1];
        self.thiStr = heroArr[2];
        self.fouStr = heroArr[3];
        self.fifStr = heroArr[4];
        
        self.botDic = [self.dic objectForKey:@"bot"];
        self.normalDic = [self.dic objectForKey:@"normal"];
        self.aramDic = [self.dic objectForKey:@"aram"];
        self.rankedDic = [self.dic objectForKey:@"ranked"];
        
        [self.tableV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    
        
    }];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
